package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipDevAppend;
import cc.comac.mom.bean.EquipDevAppendBean;
import cc.comac.mom.dao.entity.EquipDevAppendDao;
import cc.comac.mom.dao.entity.EquipDevDao;
import cc.comac.mom.entity.EquipDevAppendEntity;
import cc.comac.mom.entity.EquipDevEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipDevAppendDataHandler extends ServiceImpl<EquipDevAppendDao, EquipDevAppendEntity>{
	
	@Autowired
	private EquipDevAppendDao equipDevAppendDao;
	
	@Autowired	
	private EquipDevDao equipDevDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean save(EquipDevAppend obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAppendEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		if(innerData.getDevId() == 0) {
			throw new RuntimeException("主表信息id为空");
		}
		if(innerData.getAppendName()== null || "".equals(innerData.getAppendName())) {
			throw new RuntimeException("附属信息名称为空");
		}
		if(innerData.getDataSource()== null || "".equals(innerData.getDataSource())) {	
			throw new RuntimeException("数据源为空");
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("append_name",innerData.getAppendName());
		map.put("dev_id", innerData.getDevId());
		map.put("row_status", 1);
		List<EquipDevAppendEntity> selectByMap = equipDevAppendDao.selectByMap(map);
		if(selectByMap != null && selectByMap.size()>0) {
			throw new RuntimeException("该附属信息已经存在");
		}
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipDevAppendDao.insert(innerData);
		if(insert<1) {
			throw new RuntimeException("新增失败");
		}
		return resultBean;

	}
	
	public ResultBean update(EquipDevAppend obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAppendEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		if(innerData.getAppendName()== null || "".equals(innerData.getAppendName())) {
			throw new RuntimeException("附属信息名称为空");
		}
		if(innerData.getDevId() == 0) {
			throw new RuntimeException("主表信息id为空");
		}
		EquipDevAppendEntity selectById = equipDevAppendDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("修改的数据不存在！");
		}
		if(!selectById.getAppendName().equals(innerData.getAppendName())) {
			throw new RuntimeException("附属名称不能修改！");
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int update = equipDevAppendDao.updateById(innerData);
		if(update<1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	public ResultBean delete(EquipDevAppend obj) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAppendEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		EquipDevAppendEntity selectById = equipDevAppendDao.selectById(innerData.getId());
		if(selectById == null) {
			throw new RuntimeException("删除数据不存在");
		}
		baseEntityDataUtil.deleteBaseEntityData(selectById);
		int delete = equipDevAppendDao.updateById(selectById);
		if(delete<1) {
			throw new RuntimeException("删除失败");
		}
		return resultBean;
	}
	
	public EquipDevAppendBean selectByCondition(EquipDevAppend obj) {
    	QueryWrapper<EquipDevAppendEntity> queryWrapper = new QueryWrapper<EquipDevAppendEntity>();
    	queryWrapper.eq("row_status",1);
    	if(obj != null && obj.getInnerData() != null) {
    		if(obj.getInnerData().getDevId() != 0) {
    			ArrayList<Long> arrayList = new ArrayList<Long>();
    			arrayList.add(obj.getInnerData().getDevId());
    			EquipDevEntity equipDevEntity = equipDevDao.selectById(obj.getInnerData().getDevId());
    			if(equipDevEntity != null) {
    				arrayList.add(equipDevEntity.getDevCategoryId());
    			}
    			queryWrapper.in("dev_id",arrayList);
    		}
    		if(obj.getInnerData().getAppendName()!= null && !"".equals(obj.getInnerData().getAppendName())) {
    			queryWrapper.eq("append_name",obj.getInnerData().getAppendName());
    		}
    		if(obj.getInnerData().getDataSource()!= null && !"".equals(obj.getInnerData().getDataSource())) {
    			queryWrapper.eq("data_source",obj.getInnerData().getDataSource());
    		}
    	}
        Page<EquipDevAppendEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
        IPage<EquipDevAppendEntity> iPage = equipDevAppendDao.selectPage(page, queryWrapper);
        List<EquipDevAppendEntity> list = iPage.getRecords();
        if(list != null && list.size()>0) {
        	long pages = iPage.getPages();
        	long total = iPage.getTotal();
        	ArrayList<EquipDevAppend> arrayList = new ArrayList<EquipDevAppend>();
        	for(EquipDevAppendEntity item :list) {
        	EquipDevAppend equipDevAppend = new EquipDevAppend();
        	equipDevAppend.setInnerData(item);
        	arrayList.add(equipDevAppend);
        	}
        	EquipDevAppendBean equipDevAppendBean = new EquipDevAppendBean();
        	equipDevAppendBean.setList(arrayList);
        	equipDevAppendBean.setPageNum((int)obj.getPageNum());
        	equipDevAppendBean.setPageSize((int)obj.getPageSize());
        	equipDevAppendBean.setPages((int)pages);
        	equipDevAppendBean.setTotal((int)total);
        	return equipDevAppendBean;
        }
		return null;
		
	}
	
	public EquipDevAppend getById(long id) {
		// TODO Auto-generated method stub
		EquipDevAppendEntity appendEntity = equipDevAppendDao.selectById(id);
		if(appendEntity != null) {
			EquipDevAppend equipDevAppend = new EquipDevAppend();
			equipDevAppend.setInnerData(appendEntity);
			return equipDevAppend;
		}
		return null;
	}
}
